const path = require('path');
const HtmlWebpackPlugin = require("html-webpack-plugin");
const { CleanWebpackPlugin } = require("clean-webpack-plugin");
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
require('dotenv').config();

module.exports = function (env, argv) {
    const mode = argv.mode ?? process.env.mode ?? 'production';
    console.log('mode', mode);
    const isDevMode = mode === 'development';

    return {
        mode: isDevMode ? 'development' : 'production',
        entry: {
            index: './src/pages/index.js',
        },
        output: {
            path: path.resolve(__dirname, 'dist'),
            filename: '[name].bundle.[contenthash:8].js',
            chunkFilename: '[name].chunk.[contenthash:8].js',
        },
        module: {
            rules: [
                {
                    test: /\.css$/,
                    exclude: /node_modules/,
                    use: [
                        {
                            loader: MiniCssExtractPlugin.loader,
                        },
                        {
                            loader: 'css-loader',
                            options: {
                                importLoaders: 1,
                            }
                        },
                        {
                            loader: 'postcss-loader'
                        }
                    ]
                },
                {
                    test: /\.(png|svg|jpg|gif)$/,
                    use: [
                        {
                            loader: 'url-loader',
                            options: {
                                name: '[path][name].[ext]',
                                esModule: false,
                                limit: 1024,  // 10kb
                            }
                        },
                        /*
                        {
                            loader: 'file-loader',
                            options: {
                                name: '[path][name].[ext]',
                                esModule: false,
                            }
                        },*/
                    ],
                    //webpack5时,有点冲突,需要修改成这个类型,才可以按预期的解析
                    type: 'javascript/auto'
                },
                {
                    test: /\.(woff|ttf)$/,
                    use: [
                        {
                            loader: 'file-loader',
                            options: {
                                name: '[path][name].[ext]',
                                esModule: false,
                            }
                        },
                    ],
                    //webpack5时,有点冲突,需要修改成这个类型,才可以按预期的解析
                    type: 'javascript/auto'
                },
                {
                    test: /.\html$/,
                    //处理html中的依赖资源
                    loader: 'html-loader',
                    options: {
                        sources: {
                            list: [
                                //默认解析图片的src
                                {
                                    tag: "img",
                                    attribute: "src",
                                    type: "src",
                                },
                                //因为用了懒加载,data-src也要解析,不然图片会认为没用到
                                {
                                    tag: "img",
                                    attribute: "data-src",
                                    type: "src",
                                },
                                //响应式图片列表也要加入解析
                                {
                                    tag: "img",
                                    attribute: "data-srcset",
                                    type: "srcset",
                                },
                                {
                                    tag: "section",
                                    attribute: "data-bgset",
                                    type: "srcset",
                                },
                            ],
                        }
                    }
                },
                {
                    test: /\.m?js$/,
                    exclude: /(node_modules|bower_components)/,
                    use: {
                        loader: 'babel-loader',
                        options: {
                            presets: ['@babel/preset-env'],
                            plugins: [
                                [
                                    "@babel/plugin-transform-runtime",
                                    {
                                        "corejs": false
                                    }
                                ]
                            ]
                        }
                    }
                },
            ]
        },
        plugins: [
            new CleanWebpackPlugin(),
            new MiniCssExtractPlugin({
                filename: '[name].[contenthash:8].css',
                chunkFilename: '[name].[contenthash:8].chunk.css',
            }),
            // 创建HTML页面,webpack output主要负责提取压缩的js文件,需要用这个插件生成HTML文件
            new HtmlWebpackPlugin({
                filename: `index.html`, //生成的文件名
                template: `./src/index.html`, //文件模板不传会生成一个默认Body内容为空的HTML文件
            }),
            new HtmlWebpackPlugin({
                filename: `news.html`, //生成的文件名
                template: `./src/news.html`, //文件模板不传会生成一个默认Body内容为空的HTML文件
            }),
            new HtmlWebpackPlugin({
                filename: `news-details.html`, //生成的文件名
                template: `./src/news-details.html`, //文件模板不传会生成一个默认Body内容为空的HTML文件
            }),
            new HtmlWebpackPlugin({
                filename: `about.html`, //生成的文件名
                template: `./src/about.html`, //文件模板不传会生成一个默认Body内容为空的HTML文件
            }),
        ]
    };
};